package sv;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * Classe contenente il frame principale
 */
public class WelcomeTable extends JFrame implements ActionListener{
	private JPanel panel;
	private JPanel panel1;
	private JTextField[][] caselle = new JTextField[9][9];
	private ToolBox tb;
	private Solver solver;
	private SolverRule solverR;
	private WelcomeWindow wWindow;

	public WelcomeTable(Image image, WelcomeWindow ww) {//costruttore Welcametable
		wWindow = ww;
		
		setTitle("Welcome table");
		setSize(500, 500);
		setIconImage(image);

		panel = new JPanel();
		panel.setLayout(new GridLayout(3, 3));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10) );
		
		panel1 = new JPanel();
		
		for(int rp=0;rp<3;rp++){
			for(int cp=0;cp<3;cp++){
				//inizializzazione delle caselle
				JPanel qPanel = new JPanel();
				qPanel.setLayout(new GridLayout(3, 3));
				qPanel.setBorder(new LineBorder(Color.black,2));
				for (int r = 0; r < 3; r++){
					for (int c = 0; c < 3; c++) {
						String t="";
						/*if(r==3)
							t="1";
						if(c==4)
							t="2";*/
						caselle[r+3*rp][c+3*cp] = new JTextField(t);
						caselle[r+3*rp][c+3*cp].setHorizontalAlignment(JTextField.CENTER);
						Font font = new Font("Helvetica", Font.PLAIN, 20);
						caselle[r+3*rp][c+3*cp].setFont(font);
						caselle[r+3*rp][c+3*cp].setDocument ( new LimitedDocument ()); 
						qPanel.add(caselle[r+3*rp][c+3*cp]);
					}
				}
				panel.add(qPanel);
			}
		}

		add(panel);
		add(panel1,"South");
		JButton SolvButton=new JButton("RISOLVI");
		SolvButton.addActionListener(this);
		SolvButton.setVisible(true);
		panel1.add(SolvButton);
		
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);
	}
	
	/**
	 * Metodo che gestisce il click del button
	 * controllando che i valori in input siano corretti e 
	 * inserendoli nella matrice da passare al solver per 
	 * il calcolo della soluzione. 
	 */
	public void actionPerformed (ActionEvent e){
		Object source= e.getSource();
		//JOptionPane.showMessageDialog(panel1, "click on");
		
		//Creazione della matrice a partire dai valori nelle caselle
		// e controllo contenuto
		int[][] matrix = new int[9][9];
		for (int r = 0; r < 9; r++)
			for (int c = 0; c < 9; c++) {
				String value = caselle[r][c].getText();
				if(value.equals(""))
					matrix[r][c] = 0;
				else{
					if(value.length()>1){
						JOptionPane.showMessageDialog(null, "Il valore ("+r+","+c+") deve essere di un carattere");
						throw new IllegalArgumentException("Il valore deve essere di un carattere");
					}
					int ascii = (int)value.charAt(0);
					if(ascii>57 || ascii<48){
						JOptionPane.showMessageDialog(null, "Il valore ("+r+","+c+") deve essere un numero");
						throw new IllegalArgumentException("Il valore deve essere un numero");
					}
					matrix[r][c] = Integer.valueOf(value);
				}
				//System.out.println(matrix[r][c]);
			}
		
		//chiusura della finestra e creazione delle varie classi
		this.dispose();
		tb = new ToolBox(matrix,false);
		String s = (String)JOptionPane.showInputDialog(null,"Seleziona il solver","solver",-1,null,new String[]{"Solver1","Solver2","Solver3"},"Solver1");
		int nS = (int)s.charAt(s.length()-1)-49;
		System.out.println(nS);
		new Ris(nS);
	}
	
	class Ris extends Thread{
		private int nS;
		
		public Ris(int nS){
			this.nS=nS;
			start();
		}
		
		public void run(){
			tb.resetOver(nS);
			tb.resetPassi(nS);
			if(nS<2)
				solver = new Solver(nS,-1,false, true);
			else
				solverR = new SolverRule(-1, false, true);
			wWindow.setVisible(true);
		}
	}
	
	class LimitedDocument extends PlainDocument{
		private int limit=1;
		
		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException{
			if(getLength()<limit){
				super.insertString(offs,str,a);
			}
		}
	}
}
